import { showWin } from "./Win";

interface Piece {
    data: number;
    pic: string;
    idx: number;
    view: fgui.GComponent;
}

export class Game {

    protected _view: fgui.GComponent;

    protected _moveSpace = 250;// 每次移动，需要移动的距离
    protected _pieces: Piece[];
    protected _winStr: string = ''

    constructor(view: fgui.GComponent) {
        this._view = view;//构造函数中传入的是Game组件的显示对象
        this._pieces = [];
        for (let i = 0; i < 8; i++) {
            let piece: Piece = {
                data: i, pic: '', idx: -1, view: null
            }
            this._pieces.push(piece);
        }
        // 初始化了胜利的字符形式
        this._winStr = this._pieces.map(p => p.data + '').join(',')+',-';
        // 打乱数据
        this._pieces.sort((a, b) => Math.random() > .5 ? -1 : 1);

        // 这里注意是 <= 9 因为要将一个空的不存在的piece也放进_pieces列表
        for (let i = 0; i < 9; i++) {
            let piece = this._view.getChild(`n${i + 1}`);
            if (!piece) {
                continue;// 如果piece不存在，则跳过这次循环
            }
            this._pieces[i].idx = i;
            this._pieces[i].view = piece.asCom;
            this._pieces[i].view.getChild('title').text = '图' + (this._pieces[i].data + 1)
            piece.onClick(this, this._clickPiece, [this._pieces[i]]);
        }

        console.log(this._winStr);

        Laya.timer.once(5000,this,()=>{
            this._win()
        })
    }

    protected _chkWin() {
        let temp = this._pieces.map(p => {
            if(p)return p.data + '';
            else return '-'
        }).join(',');
        console.log(temp);

        if (this._winStr == temp) {
            // 胜利了
            this._win()
        }
    }

    protected _win() {
        console.log('win');
        showWin()
    }

    flush(){
        this._pieces.sort((a, b) => Math.random() > .5 ? -1 : 1);

        // 这里注意是 <= 9 因为要将一个空的不存在的piece也放进_pieces列表
        for (let i = 0; i < 9; i++) {
            let piece = this._view.getChild(`n${i + 1}`);
            if (!piece) {
                continue;// 如果piece不存在，则跳过这次循环
            }
            let temp = this._pieces[i];
            if(!temp) continue;
            temp.idx = i;
            temp.view = piece.asCom;
            temp.view.getChild('title').text = '图' + (temp.data + 1)
        }
    }

    protected _clickPiece(piece: Piece) {
        // this._win()
        // return;
        if (!piece.view) return;
        let tempIdxUp = piece.idx - 3
        if (tempIdxUp >= 0) {
            let p = this._pieces[tempIdxUp];
            if (!p) {
                Laya.Tween.to(piece.view, { y: piece.view.y - 250 }, 100);
                this._pieces[tempIdxUp] = piece;
                this._pieces[piece.idx] = null;
                piece.idx = tempIdxUp;
                this._chkWin()
                return;
            }
        }
        let tempIdxDown = piece.idx + 3
        if (tempIdxDown <= 8) {
            let p = this._pieces[tempIdxDown];
            if (!p) {
                Laya.Tween.to(piece.view, { y: piece.view.y + 250 }, 100);
                this._pieces[tempIdxDown] = piece;
                this._pieces[piece.idx] = null;
                piece.idx = tempIdxDown
                this._chkWin()
                return;
            }
        }
        let tempIdxLeft = piece.idx - 1
        if (piece.idx % 3 > 0) {
            let p = this._pieces[tempIdxLeft];
            if (!p) {
                Laya.Tween.to(piece.view, { x: piece.view.x - 250 }, 100);
                this._pieces[tempIdxLeft] = piece;
                this._pieces[piece.idx] = null;
                piece.idx = tempIdxLeft;
                this._chkWin()
                return;
            }
        }
        let tempIdx = piece.idx + 1
        if (piece.idx % 3 < 2) {
            let p = this._pieces[tempIdx];
            if (!p) {
                Laya.Tween.to(piece.view, { x: piece.view.x + 250 }, 100);
                this._pieces[tempIdx] = piece;
                this._pieces[piece.idx] = null;
                piece.idx = tempIdx;
                this._chkWin()
                return;
            }
        }
    }
}